package com.hna.eking.SparkUtils;

import org.apache.spark.api.java.function.ForeachFunction;
import org.apache.spark.sql.Dataset;
import org.apache.spark.sql.Row;
import org.apache.spark.sql.SparkSession;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


public class SparkRunHive {
	private String SqlStr;
	private String SessionID;
	private String UUID;
	
	private static final Logger logger = LoggerFactory.getLogger(SparkRunHive.class);

	public SparkRunHive(String SqlStr) {
		this.SqlStr = SqlStr;
	}
	public SparkRunHive() {
		
	}
	public Boolean RunSql(String SqlStr) {
		this.SqlStr = SqlStr;
		return RunSql();
	}
	public void setSql(String SqlStr) {
		this.SqlStr = SqlStr;
	}
	public Boolean RunSql() {
		String Result;
		logger.debug("run sql is: " + SqlStr);
		SparkSession spark = SparkSession.builder().enableHiveSupport().appName("EKSPARK-HIVE" + SessionID)
				.getOrCreate();
		
		Dataset<Row> ds = spark.sql(SqlStr);
		ds.foreach(new ForeachFunction<Row>(){

			public void call(Row t) throws Exception {
				// TODO Auto-generated method stub
				logger.info("row: " + t.getString(0) + "  " + t.getInt(1));
//				Result = "row: " + t.getString(0) ;
			}
			
		});
		spark.stop();
		return false;
	}
}
